<template>
	<view class="shopping-details-address">
		<u-popup :show="show" mode="bottom" :overlay="true" round="30rpx" @open="open">
			<view class="box">
				<view class="" style="height: 32rpx;"></view>
				<view class="header">
					<view class="title">配送至</view>
					<view class="icon-close" @click="close">
						<u-icon name="close" size="12" color="#BDBDBD"></u-icon>
					</view>
				</view>
				<view class="" style="height: 40rpx;"></view>

				<scroll-view scroll-y="true" class="scroll-y">
					<view>
						<view class="list">
							<view class="list-item" v-for="(item,index) in listData" :key="index"
								@click="handleActive(item)">
								<view style="height:28rpx; width:28rpx;margin-right: 12rpx;">
									<selector v-model="item.selector"></selector>
								</view>
								<view class="info">
									<view class="name">
										<text>{{item.receiver}}</text>
										<text style="margin-left: 24rpx;">{{item.userPhone}}</text>
									</view>
									<view class="address">{{item.location}} {{item.detailedAddress}}</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="btn" @click="handleAdd">新增收货地址</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import selector from "../components/selector.vue";
	import {
		addressList
	} from "@/http";
	export default {
		components: {
			selector
		},
		data() {
			return {
				show: false,
				val: false,
				userInfo: uni.getStorageSync('userInfo'),
				listData: []
			}
		},
		async mounted() {
			let res = await addressList({
				userPhone: this.userInfo.phone
			});
			this.listData = res.data.rows.map(item => {
				item.selector = false;
				return item;
			});
			this.$emit("click", this.listData[0]);
		},
		methods: {
			async init(option) {
				try {
					uni.showLoading({title: "加载中...",mask: true});
					let res = await addressList({
						userPhone: this.userInfo.phone
					});
					this.listData = res.data.rows.map(item => {
						item.selector = false;
						if (option && option.id == item.id) item.selector = true;
						return item;
					});
					uni.hideLoading();
					this.show = true;
				} catch (e) {
					uni.hideLoading();
					//TODO handle the exception
				}
			},
			handleAdd() {
				uni.navigateTo({
					url: "/myPages/address/index"
				});
				this.show = false;
			},
			handleActive(options) {
				this.show = false;
				this.listData.forEach(item => {
					item.selector = false;
					if (item.id == options.id) {
						item.selector = true;
					};
				});
				this.$emit("click", options);
				this.$emit("select", options);
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false;
				// console.log('close');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shopping-details-address {
		.scroll-y {
			height: calc(env(safe-area-inset-bottom) + 540rpx);
			border-bottom: 1px solid #EEEEEE;
		}

		.box {
			.btn {
				margin: 0 auto;
				margin-top: 10rpx;
				margin-bottom: calc(env(safe-area-inset-bottom) + 78rpx);
				width: 686rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				background: #FD8F19;
				border-radius: 40rpx;
				font-size: 32rpx;
				font-weight: 400;
				color: #FFFFFF;
			}

			.list {
				padding-left: 32rpx;
				padding-bottom: calc(env(safe-area-inset-bottom) + 168rpx);

				.list-item {
					padding: 32rpx 0;
					display: flex;
					align-items: center;

					&+.list-item {
						border-top: 1px solid #EEEEEE;
					}

					.info {
						.address {
							margin-top: 8rpx;
							font-size: 24rpx;
							font-weight: 400;
							color: #878787;
						}

						.name {
							display: flex;
							align-items: flex-end;
							font-size: 24rpx;
							font-weight: 400;
							color: #1F1F1F;
						}
					}
				}
			}

			.header {
				position: relative;

				.title {
					width: 100%;
					text-align: center;
					font-size: 30rpx;
					font-weight: 500;
					color: #1F1F1F;
				}

				.icon-close {
					position: absolute;
					right: 32rpx;
					top: 50%;
					transform: translate(0, -50%);
					background-color: #F1F1F1;
					height: 38rpx;
					width: 38rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
</style>
